﻿#region Usings

using System.Data.Entity.ModelConfiguration;
using Rail.Net.Core.Model;

#endregion

namespace Rail.Net.Data.Configuration
{
    public class TariffMap : EntityTypeConfiguration<Tariff>
    {
        public TariffMap()
        {
            ToTable("Tariffs");
            HasKey(administration => administration.Id);

            Property(administration => administration.Name).IsRequired().HasMaxLength(30);
            Property(administration => administration.ValidFrom).IsOptional();
            Property(administration => administration.ValidTo).IsOptional();
            HasMany(tariff => tariff.TravelerCombinations).WithRequired(
                travelerCombination => travelerCombination.Tariff).WillCascadeOnDelete(true);
            HasMany(tariff => tariff.Administrations).WithMany(administration => administration.Tariffs).Map(
                m =>
                    {
                        m.ToTable("TariffAdministrations");
                        m.MapLeftKey("Tariff_Id");
                        m.MapRightKey("Administration_Id");
                    });

            HasMany(tariff => tariff.Rus).WithMany(ru => ru.Tariffs).Map(
                m =>
                {
                    m.ToTable("TariffRus");
                    m.MapLeftKey("Tariff_Id");
                    m.MapRightKey("Ru_Id");
                });

            HasMany(tariff => tariff.PaymentOptions).WithMany(paymentOption => paymentOption.Tariffs).Map(
                m =>
                {
                    m.ToTable("TariffPaymentOptions");
                    m.MapLeftKey("Tariff_Id");
                    m.MapRightKey("PaymentOption_Id");
                });
        }
    }
}